#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 110;
string arr[N];
int idx[N];
int T;
int n;

LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}

LL lcm(LL a, LL b)
{
    return a * b / gcd(a, b);
}

// 板子
string minWindow(string s, string t)
{
    if (s.size() < t.size())
        return "";
    unordered_map<char, int> ws; //记录s串各个字母的数量
    unordered_map<char, int> wt; //记录t串各个字母的数量
    int slow = 0;
    int count = 0; //记录窗口中（slow和fast区间内）已包含t串字符的数量
    string result;

    for (int i = 0; i < t.size(); i++)
    { //遍历t串保存其各个字母的数量
        wt[t[i]]++;
    }

    for (int fast = 0; fast < s.size(); fast++)
    { //fast指针遍历s串
        ws[s[fast]]++;
        if (ws[s[fast]] <= wt[s[fast]])
            count++; //如果当前窗口中该字母的数量还未达到t中的数量要求，则必须加入

        while (count == t.size())
        { //count=t.size()即窗口中全部满足t的各个字母的数量要求
            int len = fast - slow + 1;
            if (result.size() == 0 || len < result.size())
            { //要考虑第一次的情况
                result.clear();
                result = s.substr(slow, len);
            }
            ws[s[slow]]--;
            if (ws[s[slow]] < wt[s[slow]])
                count--; //slow指针进行收缩，一旦必须要求的字母数量减少，count--
            slow++;
        }
    }
    return result;
}

inline void solution()
{
    cin >> T;
    while (T--)
    {
        cin >> n;
        string res = "";
        mm(idx, 0);
        unordered_set<char> st;
        LL len = 1;
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
            len = lcm(len, arr[i].size());
        }
        len = len * 2 * n;
        while (res.size() <= len)
        {   
            for (int i = 0; i < n; i++)
            {
                int id = idx[i];
                res += arr[i][id];
                st.insert(arr[i][id]);
                idx[i]++;
                if (idx[i] >= arr[i].size())
                    idx[i] = 0;
            }
        }
        string t = "";
        for (auto &s : st)
            t += s;
        // cout << res << endl << t << endl;
        cout << minWindow(res, t).size() << endl;
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}